from django.db import models
from dvadmin.utils.models import CoreModel, table_prefix, get_custom_app_models


# Create your models here.
class Knowledge(CoreModel):
    name = models.CharField(max_length=64, verbose_name="名称", help_text="名称")
    TYPE_CHOICES = (
        (1, "文档知识库"),
        (2, "Excel知识库"),
        (3, "DB知识库"),
    )
    type = models.IntegerField(
        choices=TYPE_CHOICES, default=1, verbose_name="类型", null=True, blank=True, help_text="类型"
    )
    VALID_CHOICES = (
        (0, "无效"),
        (1, "有效"),
    )
    valid = models.IntegerField(
        choices=VALID_CHOICES, default=1, verbose_name="有效", null=True, blank=True, help_text="有效"
    )

    class Meta:
        db_table = table_prefix + "knowledge"
        verbose_name = "知识库表"
        verbose_name_plural = verbose_name
        ordering = ("-update_datetime",)

class KnowledgeRecordConfig(models.Model):
    kid = models.IntegerField(verbose_name="kid", help_text="kid")
    SEGMENT_CHOICES = (
        (1, "自动分段"),
        (2, "自定义分段"),
        (3, "目录分段"),
    )
    segment_type = models.IntegerField(
        choices=SEGMENT_CHOICES, default=1, verbose_name="分段方式", null=True, blank=True, help_text="分段方式"
    )
    TYPE_CHOICES = (
        (1, "等价分段"),
        (2, "递归分段"),
    )
    type = models.IntegerField(
        choices=TYPE_CHOICES, default=1, verbose_name="分段策略", null=True, blank=True, help_text="分段策略"
    )
    split_CHOICES = (
        ("\n", "换行"),
        ("\n\n", "\n\n"),
        ("。", "中文句号"),
    )
    split_c = models.CharField(
        choices=split_CHOICES, max_length=10, default="\n", verbose_name="分段分隔符", help_text="分段分隔符"
    )
    split_reg = models.CharField(max_length=200,verbose_name="正则分隔符", help_text="正则分隔符")
    chunk_size = models.IntegerField(default=0,verbose_name="分段长度", help_text="分段长度")
    repeat_size = models.IntegerField(default=0,verbose_name="分段重复长度", help_text="分段重复长度")    
    VECTOR_CHOICES = (
        (1, "等价分段"),
        (2, "递归分段"),
    )
    vector = models.IntegerField(
        choices=VECTOR_CHOICES, default=1, verbose_name="向量服务", null=True, blank=True, help_text="向量服务"
    )
    class Meta:
        db_table = table_prefix + "Knowledge_record_config"
        verbose_name = "知识库配置表"
        verbose_name_plural = verbose_name

class KnowledgeRecord(CoreModel):
    kid = models.IntegerField(verbose_name="kid", help_text="kid")
    name = models.CharField(max_length=64, null=True, blank=True, verbose_name="名称", help_text="名称")
    TYPE_CHOICES = (
        (1, "txt"),
        (2, "DOC"),
        (3, "DOCX"),
        (4, "PDF"),
        (5, "WPS"),
        (6, "IMG"),
    )
    type = models.IntegerField(
        choices=TYPE_CHOICES, default=1, verbose_name="类型", null=True, blank=True, help_text="类型"
    )
    SOURCE_CHOICES = (
        (1, "文件"),
        (2, "接口"),
    )
    source = models.IntegerField(
        choices=SOURCE_CHOICES, default=1, verbose_name="来源", null=True, blank=True, help_text="来源"
    )
    STATUS_CHOICES = (
        (0, "失败"),
        (1, "成功"),
        (2, "处理中"),
    )
    status = models.IntegerField(
        choices=STATUS_CHOICES, default=2, verbose_name="成功", null=True, blank=True, help_text="成功"
    )
    VALID_CHOICES = (
        (0, "无效"),
        (1, "有效"),
    )
    valid = models.IntegerField(
        choices=VALID_CHOICES, default=1, verbose_name="有效", null=True, blank=True, help_text="有效"
    )
    num_char = models.IntegerField(default=0,verbose_name="字符数", help_text="字符数")     
    num_chunk = models.IntegerField(default=0,verbose_name="分段数", help_text="分段数")
    num_hit = models.IntegerField(default=0,verbose_name="命中数", help_text="命中数")  
    class Meta:
        db_table = table_prefix + "Knowledge_record"
        verbose_name = "文件表"
        verbose_name_plural = verbose_name
    

class KnowledgeRecordSegment(models.Model):
    id = models.BigAutoField(primary_key=True, help_text="Id", verbose_name="Id")
    rid = models.IntegerField(verbose_name="rid", help_text="rid")
    kid = models.IntegerField(verbose_name="kid", help_text="kid")
    no_chunk = models.IntegerField(default=1,verbose_name="分段序号", help_text="分段序号")
    content = models.TextField(verbose_name="内容", help_text="内容")
    num_char = models.IntegerField(default=0,verbose_name="字符数", help_text="字符数")   
    num_hit = models.IntegerField(default=0,verbose_name="命中数", help_text="命中数")  
    VALID_CHOICES = (
        (0, "无效"),
        (1, "有效"),
    )
    valid = models.IntegerField(
        choices=VALID_CHOICES, default=1, verbose_name="有效", null=True, blank=True, help_text="有效"
    )
    class Meta:
        db_table = table_prefix + "Knowledge_record_segment"
        verbose_name = "文件分块表"
        verbose_name_plural = verbose_name

class KnowledgeHit(CoreModel):
    kid = models.IntegerField(verbose_name="kid", help_text="kid")
    keyword = models.CharField(max_length=200, null=True, blank=True, verbose_name="关键词", help_text="关键词")
    topk = models.IntegerField(verbose_name="topk", help_text="topk")
    class Meta:
        db_table = table_prefix + "Knowledge_hit"
        verbose_name = "命中测试表"
        verbose_name_plural = verbose_name
        
class KnowledgeHitItem(models.Model):
    hid = models.IntegerField(verbose_name="hid", help_text="hid")
    fid = models.IntegerField(verbose_name="fid", help_text="fid")
    cid = models.IntegerField(verbose_name="cid", help_text="cid")
    context = models.CharField(max_length=200, null=True, blank=True, verbose_name="关键词", help_text="关键词")
    
    class Meta:
        db_table = table_prefix + "Knowledge_hit_item"
        verbose_name = "命中测试明细表"
        verbose_name_plural = verbose_name
